<html>
<!-- Head tag -->
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	
	
		<link href="/favicon.ico" rel="icon">
	 
      <title>Struts2+DataTables动态生成数据表 | BiJii&#39;s Blog</title>
	<link rel="stylesheet" href="/css/font-awesome/css/font-awesome.css">
	<link rel="stylesheet" href="/css/style.css">
	<link rel="stylesheet" href="/css/highlight.css">
	
    <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?542b4962c3c4035fce9071b7c665f0d2";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>

	
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-97838175-1', 'auto');
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->


</head>
<body>
	<div id="site" class="site">
		<div id="sidebar" class="sidebar">
			<header id="header" class="site-header">
	<div class="site-branding">
		<h1 class="site-title"><a href="/" rel="home">BiJii&#39;s Blog</a></h1>
		<p class="site-description">认认真真...</p>
		<button class="secondary-toggle font-asesome-icon">Menu and widgets</button>
	</div>
</header>
<div id="secondary" class="secondary">
	<nav class="main-navigation">
                         <ul id="menu-demo-menu" class="nav-menu">
						 
							<li class="menu-item"><a href="/">首页</a></li>
						
							<li class="menu-item"><a href="/archives">归档</a></li>
						
							<li class="menu-item"><a href="/about">关于</a></li>
						
                         </ul>
    </nav>
	
		
<aside class="widget">
		<h3 class="widget-title">归档</h3>		
		<ul>
			<ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">1</span></li></ul>
		</ul>
	</aside>


	
		
<aside class="widget">
		<h3 class="widget-title">最新文章</h3>		
		<ul>
			
          <li>
            <a href="/2017/12/08/TeiidUseNote/">Teiid搭建和使用小记</a>
          </li>
        
          <li>
            <a href="/2017/11/16/HexoOSChina/">Hexo+OSChina搭建免费个人博客</a>
          </li>
        
          <li>
            <a href="/2017/06/12/DatatablesStruts/">Struts2+DataTables动态生成数据表</a>
          </li>
        
		</ul>
	</aside>


	
		
  <aside class="widget">
		<h3 class="widget-title">标签云</h3>
        <a href="/tags/Datatables/" style="font-size: 10px;">Datatables</a> <a href="/tags/Hexo/" style="font-size: 10px;">Hexo</a> <a href="/tags/Teiid/" style="font-size: 10px;">Teiid</a>
    </aside>

	
</div>
		</div>
		<div id="content" class="site-content">
			<main id="main" class="site-main" role="main">
				
<article class="hentry ">
		
		
			<header class="entry-header">
				<h2 class="entry-title"><a href="/2017/06/12/DatatablesStruts/" rel="bookmark">Struts2+DataTables动态生成数据表</a></h2>	
			</header>
		
		<!-- .entry-header -->
		<div class="entry-content">
			
				<blockquote>
<p><a href="https://datatables.net/manual/server-side" target="_blank" rel="external">Server-side processing</a></p>
<ul>
<li>新建返回参数变量、部分发送参数变量，以及set和get方法</li>
<li>在struts的action方法中，根据传入的发送参数，生成返回参数变量值<br><img src="https://git.oschina.net/vfface/Markdown-Photos/raw/master/datatables-net-manual-server-side.png?dir=0&amp;filepath=datatables-net-manual-server-side.png&amp;oid=f4f8603ce046180cffc8484314d82a63c441efff&amp;sha=563d1aa7ac234e0df354167efced2d123c994d2d" alt="image"></li>
</ul>
<hr>
<h4 id="Html-Datatables-JS代码"><a href="#Html-Datatables-JS代码" class="headerlink" title="Html Datatables JS代码"></a>Html Datatables JS代码</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">&lt;script type=&quot;text/javascript&quot;&gt;</div><div class="line">$(document).ready(function () &#123;</div><div class="line">    $(&quot;#companies&quot;).dataTable(&#123;</div><div class="line">        &quot;processing&quot;: true,</div><div class="line">        &quot;serverSide&quot;: true,</div><div class="line">        &quot;ajax&quot;: &quot;selectOptionAjaxRequest.action&quot;,</div><div class="line">    	&quot;columnDefs&quot;: [ &#123; orderable: true, targets: [0,1,2] &#125;],</div><div class="line">        &quot;sPaginationType&quot;: &quot;full_numbers&quot;, </div><div class="line">        &quot;bFilter&quot;: false,</div><div class="line">        &quot;columns&quot;: [</div><div class="line">           &#123; &quot;data&quot;: &quot;name&quot; &#125;,</div><div class="line">           &#123; &quot;data&quot;: &quot;address&quot; &#125;,</div><div class="line">           &#123; &quot;data&quot;: &quot;town&quot; &#125;</div><div class="line">           ]</div><div class="line">    &#125;);</div><div class="line">&#125;);</div><div class="line">&lt;/script&gt;</div></pre></td></tr></table></figure>
</blockquote>
<hr>
<h4 id="Struts-Action-Java代码"><a href="#Struts-Action-Java代码" class="headerlink" title="Struts Action Java代码"></a>Struts Action Java代码</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div></pre></td><td class="code"><pre><div class="line">package com.ibm.cxl.action;</div><div class="line"></div><div class="line">import java.util.ArrayList;</div><div class="line">import java.util.Arrays;</div><div class="line">import java.util.Collections;</div><div class="line">import java.util.Comparator;</div><div class="line">import java.util.List;</div><div class="line">import java.util.Map;</div><div class="line"></div><div class="line">import com.opensymphony.xwork2.ActionContext;</div><div class="line">import com.opensymphony.xwork2.ActionSupport;</div><div class="line"></div><div class="line">import jquery.datatables.model.Company;</div><div class="line">import jquery.datatables.model.DataRepository;</div><div class="line"></div><div class="line">public class RptConfig01Action extends ActionSupport  &#123;</div><div class="line">	</div><div class="line">	private static final long serialVersionUID = -7160474110317441632L;</div><div class="line"></div><div class="line">	private Integer draw=0, recordsTotal=1, recordsFiltered = 0, length = 0, start=0;</div><div class="line">	private List&lt;Company&gt; data=new ArrayList&lt;Company&gt;();</div><div class="line">	</div><div class="line">	public Integer getDraw() &#123;</div><div class="line">		return draw;</div><div class="line">	&#125;</div><div class="line">	public void setDraw(Integer draw) &#123;</div><div class="line">		this.draw = draw;</div><div class="line">	&#125;</div><div class="line">	public Integer getRecordsTotal() &#123;</div><div class="line">		return recordsTotal;</div><div class="line">	&#125;</div><div class="line">	public void setRecordsTotal(Integer recordsTotal) &#123;</div><div class="line">		this.recordsTotal = recordsTotal;</div><div class="line">	&#125;</div><div class="line">	public Integer getRecordsFiltered() &#123;</div><div class="line">		return recordsFiltered;</div><div class="line">	&#125;</div><div class="line">	public void setRecordsFiltered(Integer recordsFiltered) &#123;</div><div class="line">		this.recordsFiltered = recordsFiltered;</div><div class="line">	&#125;</div><div class="line">	public Integer getLength() &#123;</div><div class="line">		return length;</div><div class="line">	&#125;</div><div class="line">	public void setLength(Integer length) &#123;</div><div class="line">		this.length = length;</div><div class="line">	&#125;</div><div class="line">	public Integer getStart() &#123;</div><div class="line">		return start;</div><div class="line">	&#125;</div><div class="line">	public void setStart(Integer start) &#123;</div><div class="line">		this.start = start;</div><div class="line">	&#125;</div><div class="line">	</div><div class="line"></div><div class="line">	@SuppressWarnings(&quot;unchecked&quot;)</div><div class="line">	public String execute() throws Exception &#123;</div><div class="line">		</div><div class="line">		recordsTotal=DataRepository.GetCompanies().size();</div><div class="line">		recordsFiltered = recordsTotal;</div><div class="line">		</div><div class="line">		//print all parameters from the action context</div><div class="line">		Map&lt;String, Object&gt; map = ActionContext.getContext().getParameters();</div><div class="line">		for (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) &#123;</div><div class="line">		    String key = entry.getKey();</div><div class="line">		    Object value = entry.getValue();</div><div class="line">		    System.out.println(&quot;key:&quot;+ key +&quot; value: &quot; + Arrays.toString((String[])value));		    </div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		final int sortColumnIndex = Integer.parseInt(((String[]) map.get(&quot;order[0][column]&quot;))[0]);</div><div class="line">		final int sortDirection = ((String[]) map.get(&quot;order[0][dir]&quot;))[0].equals(&quot;asc&quot;) ? -1 : 1;		</div><div class="line">		</div><div class="line">		for(Company c : DataRepository.GetCompanies())&#123;</div><div class="line">			data.add(c);</div><div class="line">		&#125;</div><div class="line"></div><div class="line">		Collections.sort(data, new Comparator&lt;Company&gt;()&#123;</div><div class="line">			@Override</div><div class="line">			public int compare(Company c1, Company c2) &#123;	</div><div class="line">				switch(sortColumnIndex)&#123;</div><div class="line">				case 0:</div><div class="line">					return c1.getName().compareTo(c2.getName()) * sortDirection;</div><div class="line">				case 1:</div><div class="line">					return c1.getAddress().compareTo(c2.getAddress()) * sortDirection;</div><div class="line">				case 2:</div><div class="line">					return c1.getTown().compareTo(c2.getTown()) * sortDirection;</div><div class="line">				&#125;</div><div class="line">				return 0;</div><div class="line">			&#125;</div><div class="line">		&#125;);</div><div class="line">		</div><div class="line">		if(recordsTotal&lt; start + length) &#123;</div><div class="line">			data = data.subList(start, length);</div><div class="line">		&#125; else &#123;</div><div class="line">			data = data.subList(start, start + length);</div><div class="line">		&#125;</div><div class="line">		</div><div class="line">		return SUCCESS;</div><div class="line">        &#125;</div><div class="line">	public List&lt;Company&gt; getData() &#123;</div><div class="line">		return data;</div><div class="line">	&#125;</div><div class="line">	public void setData(List&lt;Company&gt; data) &#123;</div><div class="line">		this.data = data;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>

			
		</div><!-- .entry-content -->
		
			<div class="entry-comments">
				 
  
   
      <!-- ��˵���ۿ� start -->
	<div class="ds-thread" data-thread-key="2017/06/12/DatatablesStruts/" data-title="Struts2+DataTables动态生成数据表" data-url="http://vfface.oschina.io/2017/06/12/DatatablesStruts/"></div>
<!-- ��˵���ۿ� end -->
<!-- ��˵����JS���� start (һ����ҳֻ������һ��) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"troyyang"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.unstable.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0] 
		 || document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
	</script>
<!-- ��˵����JS���� end -->
 
			</div>
		
		<footer class="entry-footer">
			<span class="posted-on font-asesome-icon">
	<a href="" rel="bookmark">
	<time class="updated" datetime="2017-06-11T16:00:00.000Z">2017-06-12</time>
	</a>
</span>

	<span class="cat-links font-asesome-icon">
		
				<a href="/categories/前端图表/" rel="category tag">前端图表</a>
		
	</span>


	<span class="tags-links font-asesome-icon">
	
		<a href="/tags/Datatables/" rel="category tag">Datatables</a>
    
</span>
		

    <span class="eye font-asesome-icon" >
         <span id="/2017/06/12/DatatablesStruts/" class="leancloud_visitors" data-flag-title="Struts2+DataTables动态生成数据表">
        
        </span>
    </span>

		</footer><!-- .entry-footer -->
</article>
<div class="misc">
    <a href="#main"><span class="top font-asesome-icon">Top</span></a>
</div>
			</main>
		</div>
		<footer id="colophon" class="site-footer">
			<div class="site-info">
				<span>powered by <a href="https://hexo.io/">Hexo</a>&nbsp;&nbsp; theme by <a href="https://github.com/Troy-Yang/hexo-theme-twentyfifteen-wordpress">Troy</a> &nbsp; inspired by <a href="https://wordpress.org/themes/">wordpress</a></span>
			</div><!-- .site-info -->
		</footer>
	</div>
    <!-- After footer scripts -->
    <script src="/js/jquery-3.1.1.min.js"></script>
<script src="/js/main.js"></script>

    <!--referring from https://notes.wanghao.work/2015-10-21-%E4%B8%BANexT%E4%B8%BB%E9%A2%98%E6%B7%BB%E5%8A%A0%E6%96%87%E7%AB%A0%E9%98%85%E8%AF%BB%E9%87%8F%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD.html -->
    <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
    <script>
        AV.initialize("wzqncxgqp9MCPiPERe3gnX2Q-gzGzoHsz", "CCtBiqyPtJWupuozt1393AXm");
    </script>
    <script>
        function showTime(Counter) {
            var query = new AV.Query(Counter);
            $(".leancloud_visitors").each(function () {
                var url = $(this).attr("id").trim();
                query.equalTo("url", url);
                query.find({
                    success: function (results) {
                        if (results.length == 0) {
                            var content = $(document.getElementById(url)).text() + ': 0';
                            $(document.getElementById(url)).text(content);
                            return;
                        }
                        for (var i = 0; i < results.length; i++) {
                            var object = results[i];
                            var content = $(document.getElementById(url)).text() + ': ' + object.get('time');
                            $(document.getElementById(url)).text(content);
                        }
                    },
                    error: function (object, error) {
                        console.log("Error: " + error.code + " " + error.message);
                    }
                });
            });
        }
        function addCount(Counter) {
            var Counter = AV.Object.extend("Counter");
            url = $(".leancloud_visitors").attr('id').trim();
            title = $(".leancloud_visitors").attr('data-flag-title').trim();
            var query = new AV.Query(Counter);
            query.equalTo("url", url);
            query.find({
                success: function (results) {
                    if (results.length > 0) {
                        var counter = results[0];
                        counter.fetchWhenSave(true);
                        counter.increment("time");
                        counter.save(null, {
                            success: function (counter) {
                                var content = $(document.getElementById(url)).text() + ': ' + counter.get('time');
                                // remove ': '
                                $(document.getElementById(url)).text(counter.get('time'));
                            },
                            error: function (counter, error) {
                                console.log('Failed to save Visitor num, with error message: ' + error.message);
                            }
                        });
                    } else {
                        var newcounter = new Counter();
                        newcounter.set("title", title);
                        newcounter.set("url", url);
                        newcounter.set("time", 1);
                        newcounter.save(null, {
                            success: function (newcounter) {
                                console.log("newcounter.get('time')=" + newcounter.get('time'));
                                var content = $(document.getElementById(url)).text() + ': ' + newcounter.get('time');
                                // remove ': '
                                $(document.getElementById(url)).text(newcounter.get('time'));
                            },
                            error: function (newcounter, error) {
                                console.log('Failed to create');
                            }
                        });
                    }
                },
                error: function (error) {
                    console.log('Error:' + error.code + " " + error.message);
                }
            });
        }
        $(function () {
            var Counter = AV.Object.extend("Counter");
            if ($('.leancloud_visitors').length == 1) {
                addCount(Counter);
            } else if ($('.post-title-link').length > 1) {
                showTime(Counter);
            }
        });
    </script>
    
</body>
</html>